package com.exemplo.flickrsocial.service;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.exemplo.flickrsocial.view.FlickrHelper;
import com.exemplo.flickrsocial.view.listImageAdapter;
import com.gmail.yuyang226.flickr.Flickr;
import com.gmail.yuyang226.flickr.oauth.OAuth;
import com.gmail.yuyang226.flickr.oauth.OAuthToken;
import com.gmail.yuyang226.flickr.people.User;
import com.gmail.yuyang226.flickr.photos.PhotoList;

public class LoadPhotostreamTask extends AsyncTask<OAuth, Void, PhotoList> {

	/**
	 * 
	 */
	private ListView listView;
	private Activity activity;

	/**
	 * 
	 * @param activity
	 * @param listView
	 */
	public LoadPhotostreamTask(Activity activity, ListView listView) {
		this.activity = activity;
		this.listView = listView;
	}

	@Override
	protected PhotoList doInBackground(OAuth... oauth) {
		OAuthToken token = oauth[0].getToken();
		Flickr flickr = FlickrHelper.getInstance().getFlickrAuthed(
				token.getOauthToken(), token.getOauthTokenSecret());
		Set<String> extras = new HashSet<String>();
		extras.add("url_sq");
		extras.add("url_l");
		extras.add("views");
		User user = oauth[0].getUser();

		try {

			return flickr.getPeopleInterface().getPhotos(user.getId(), extras,
					20, 1);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(PhotoList photoList) {
		super.onPostExecute(photoList);

		if (photoList != null) {
			listImageAdapter adapter = new listImageAdapter(this.activity, photoList);
			this.listView.setAdapter(adapter);
		}
	}

}